﻿#include "AnimationSettingDialog.h"
#include "ui_AnimationSettingDialog.h"

#include "AnimationSettingData.h"
#include <QIntValidator>
#include "IdaWidgetHelper.h"
#include <QDebug>

namespace pst
{
    AnimationSettingDialog::AnimationSettingDialog(QWidget* parent)
        : IdaDialogFrame(parent)
        , ui(new Ui::AnimationSettingDialog())
        , m_currentData{ nullptr }
    {
        ui->setupUi(this);

        setWindowTitle(tr("动态设置"));
        //只能输入正整数
        //QIntValidator* intValidator = new QIntValidator(1, 1000000, ui->lineEdit_sampleRate);
        //ui->lineEdit_sampleRate->setValidator(intValidator);
        //intValidator = new QIntValidator(1, 45, ui->lineEdit_frameRate);
        //ui->lineEdit_frameRate->setValidator(intValidator);

        //只能输入 double 数字
        QRegularExpressionValidator* validator = new QRegularExpressionValidator(
            QRegularExpression(R"(^(|0(\.\d{0,2})?|([1-9]?\d)(\.\d{0,2})?|(1\d{2}|2[0-9]{2}|3[0-5]\d)(\.\d{0,2})?|360(\.0{0,2})?)$)"),
            ui->lineEdit_thetaAngle);
        ui->lineEdit_thetaAngle->setValidator(validator);
        validator = new QRegularExpressionValidator(
            QRegularExpression(R"(^(|0(\.\d{0,2})?|([1-9]?\d)(\.\d{0,2})?|(1\d{2}|2[0-9]{2}|3[0-5]\d)(\.\d{0,2})?|360(\.0{0,2})?)$)"),
            ui->lineEdit_thetaAngle);
        ui->lineEdit_phiAngle->setValidator(validator);
        IdaWidgetHelperInstance->setMainButtonStyle(ui->pushButton_confirm); // 将确定设置为主按钮

        QObject::connect(ui->lineEdit_thetaAngle, &QLineEdit::editingFinished, [=]()
                         {
            if (ui->lineEdit_thetaAngle->text().isEmpty()) 
            {
                ui->lineEdit_thetaAngle->setText("0");
                return;
            }
            bool ok;

            QString thetaText = ui->lineEdit_thetaAngle->text();
            if (thetaText.endsWith(".")) 
            {
                thetaText += "0";
                ui->lineEdit_thetaAngle->setText(thetaText);
            }

            double value = thetaText.toDouble(&ok);
    
            if (!ok) {
                ui->lineEdit_thetaAngle->setText("0"); // 非数字时重置为0
            } else if (value > 360) {
                ui->lineEdit_thetaAngle->setText("360"); // 超过上限时设为360
            } else if (value < 0) {
                ui->lineEdit_thetaAngle->setText("0"); // 低于下限时设为0
            } });

        QObject::connect(ui->lineEdit_phiAngle, &QLineEdit::editingFinished, [=]()
                         {
            if (ui->lineEdit_phiAngle->text().isEmpty()) 
            {
                ui->lineEdit_phiAngle->setText("0");
                return;
            }
            bool ok;
            QString phiText = ui->lineEdit_phiAngle->text();
            if (phiText.endsWith("."))
            {
                phiText += "0";
                ui->lineEdit_phiAngle->setText(phiText);
            }

            double value = phiText.toDouble(&ok);
    
            if (!ok) {
                ui->lineEdit_phiAngle->setText("0"); // 非数字时重置为0
            } else if (value > 360) {
                ui->lineEdit_phiAngle->setText("360"); // 超过上限时设为360
            } else if (value < 0) {
                ui->lineEdit_phiAngle->setText("0"); // 低于下限时设为0
            } });
}


    void AnimationSettingDialog::updateUI()
    {
        if (m_isDynamicTypeCamera)//相机播放的时候【角度动图设置】可点击，可编辑，【数据动图设置】应禁用，不可编辑
        {
            ui->groupBox->setEnabled(true);
            ui->groupBox_2->setEnabled(false);
        }
        else//数据播放的时候【数据动图设置】可点击，可编辑，【角度动图设置】应禁用，不可编辑
        {
            ui->groupBox->setEnabled(false);
            ui->groupBox_2->setEnabled(true);
        }
    }

    AnimationSettingDialog::~AnimationSettingDialog()
    {
        delete ui;
    }

    void AnimationSettingDialog::updateSetting(AnimationSettingData* data)
    {
        if (data == nullptr)
        {
            qDebug() << "AnimationSettingDialog::updateSetting data is nullptr";
            return;
        }
        m_currentData = data;
        ui->radioButton_rorateTheta->setChecked(data->isThetaChecked());
        ui->radioButton_rotatePhi->setChecked(data->isPhiChecked());
        m_thetaAngle = data->thetaAngle();
        ui->lineEdit_thetaAngle->setText(QString::number(m_thetaAngle));
        m_phiAngle = data->phiAngle();
        ui->lineEdit_phiAngle->setText(QString::number(m_phiAngle));
        //ui->lineEdit_sampleRate->setText(QString::number(data->sampleRate()));
        //ui->lineEdit_frameRate->setText(QString::number(data->frameRate()));
        ui->spinBox_sampleRate->setValue(data->sampleRate());
        ui->spinBox_frameRate->setValue(data->frameRate());
    }

    void AnimationSettingDialog::setIsDynamicTypeCamera(bool isDynamicTypeCamera)
    {
        m_isDynamicTypeCamera = isDynamicTypeCamera;
        updateUI();
    }

    //void AnimationSettingDialog::on_radioButton_rorateTheta_clicked(bool checked)
    //{

    //}

    //void AnimationSettingDialog::on_radioButton_roratePhi_clicked(bool checked)
    //{
    //}

    void AnimationSettingDialog::on_pushButton_confirm_clicked()
    {
        if (m_currentData == nullptr)
        {
            qDebug() << "AnimationSettingDialog::on_pushButton_confirm_clicked m_currentData is nullptr";
            this->close();
        }
        m_currentData->setThetaChecked(ui->radioButton_rorateTheta->isChecked());
        m_currentData->setPhiChecked(ui->radioButton_rotatePhi->isChecked());

        QString thetaAngleText = ui->lineEdit_thetaAngle->text();
        thetaAngleText = thetaAngleText.trimmed().isEmpty() ? "0" : thetaAngleText;
        ui->lineEdit_thetaAngle->setText(thetaAngleText);

        QString phiAngleText = ui->lineEdit_phiAngle->text();
        phiAngleText = phiAngleText.trimmed().isEmpty() ? "0" : phiAngleText;
        ui->lineEdit_phiAngle->setText(phiAngleText);

        double thetaAngle = ui->lineEdit_thetaAngle->text().toDouble();
        double phiAngle = ui->lineEdit_phiAngle->text().toDouble();
        m_currentData->setThetaAngle(thetaAngle);
        m_currentData->setPhiAngle(phiAngle);
        m_currentData->setSampleRate(ui->spinBox_sampleRate->value());
        m_currentData->setFrameRate(ui->spinBox_frameRate->value());

        this->close();
    }

    void AnimationSettingDialog::on_pushButton_cancel_clicked()
    {
        this->close();
    }
}
